package com.lisx.module_memo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.db.MemoDbEntity;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.manager.MemoManager;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.lisx.module_memo.R;
import com.lisx.module_memo.databinding.ActivityAddMemoBinding;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AddMemoActivity extends BaseActivity {

    private ActivityAddMemoBinding binding;
    private int ptType;
    private MemoDbEntity memoDbEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAddMemoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Intent intent = getIntent();
        ptType = intent.getIntExtra("ptType", 1);
        memoDbEntity = intent.getParcelableExtra("data");
        if (memoDbEntity != null) {
            binding.etTitle.setText(memoDbEntity.getTitle());
            binding.etContent.setText(memoDbEntity.getMsg());
            binding.tvTitle.setText("编辑备忘录");
        }
        binding.ivFanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.tvComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();
            }
        });
    }

    private void saveData() {
        String title = binding.etTitle.getText().toString();
        String content = binding.etContent.getText().toString();
        if (TextUtils.isEmpty(title)) {
            ToastUtils.showShort("请输入标题");
            return;
        }
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort("请输入内容");
            return;
        }
        if (memoDbEntity != null) {
            memoDbEntity.setRecord_time(System.currentTimeMillis());
            memoDbEntity.setTitle(title);
            memoDbEntity.setMsg(content);
        }

        MemoDbEntity entity = new MemoDbEntity();
        entity.setCreate_time(System.currentTimeMillis());
        entity.setMsg(content);
        entity.setTitle(title);
        entity.setRecord_time(System.currentTimeMillis());
        entity.setType(ptType);
        Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Boolean> emitter) throws Exception {
                if (memoDbEntity == null) {
                    MemoManager.getInstance().saveMemoDbEntity(entity);
                } else {
                    MemoManager.getInstance().saveMemoDbEntity(memoDbEntity);
                }
                emitter.onNext(true);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Boolean aBoolean) {
                        EventBus.getDefault().post(new EventEntity(EventBusConstants.UPDATE_MEMO));
                        if (memoDbEntity == null) {
                            ToastUtils.showShort("添加成功");
                        } else {
                            ToastUtils.showShort("修改成功");
                        }
                        finish();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (memoDbEntity == null) {
                            ToastUtils.showShort("添加失败");
                        } else {
                            ToastUtils.showShort("修改失败");
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
